package com.wuye.letter.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wuye.basic.domain.WyOwner;
import com.wuye.basic.service.IWyHomeService;
import com.wuye.basic.service.IWyOwnerService;
import com.wuye.common.utils.MailUtil;
import com.wuye.letter.mapper.WyLetterMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuye.common.annotation.Log;
import com.wuye.common.core.controller.BaseController;
import com.wuye.common.core.domain.AjaxResult;
import com.wuye.common.enums.BusinessType;
import com.wuye.letter.domain.WyLetter;
import com.wuye.letter.service.IWyLetterService;
import com.wuye.common.utils.poi.ExcelUtil;
import com.wuye.common.core.page.TableDataInfo;

/**
 * 短信记录Controller
 * 
 * @author avalon
 * @date 2024-11-27
 */
@RestController
@RequestMapping("/letterManage/letter")
public class WyLetterController extends BaseController
{
    @Autowired
    private IWyLetterService wyLetterService;
    @Autowired
    private IWyOwnerService wyOwnerService;


    /**
     * 查询短信记录列表
     */
    @PreAuthorize("@ss.hasPermi('letterManage:letter:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyLetter wyLetter)
    {
        startPage();
        List<WyLetter> list = wyLetterService.selectWyLetterList(wyLetter);
        list.forEach(wyLetter1 -> {
            wyLetter1.setOwner(wyOwnerService.selectWyOwnerByOwnerId(wyLetter1.getOwnerId()));

        });
        return getDataTable(list);
    }

    /**
     * 导出短信记录列表
     */
    @PreAuthorize("@ss.hasPermi('letterManage:letter:export')")
    @Log(title = "短信记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyLetter wyLetter)
    {
        List<WyLetter> list = wyLetterService.selectWyLetterList(wyLetter);
        ExcelUtil<WyLetter> util = new ExcelUtil<WyLetter>(WyLetter.class);
        util.exportExcel(response, list, "短信记录数据");
    }

    /**
     * 获取短信记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('letterManage:letter:query')")
    @GetMapping(value = "/{letterId}")
    public AjaxResult getInfo(@PathVariable("letterId") Long letterId)
    {
        return success(wyLetterService.selectWyLetterByLetterId(letterId));
    }

    /**
     * 新增短信记录
     */
    @PreAuthorize("@ss.hasPermi('letterManage:letter:add')")
    @Log(title = "短信记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyLetter wyLetter)
    {

        wyLetter.setCreateTime(new Date());
        Long[] ownerList = wyLetter.getOwnerList();
        int length = ownerList.length;
        int i = 0;
        for (Long ownerId : ownerList){
            wyLetter.setOwnerId(ownerId);
            wyLetterService.insertWyLetter(wyLetter);
            i++;
        }
        if (i==length) {
            return AjaxResult.success("发送成功");
        }
        return AjaxResult.warn("发送失败");
    }

    /**
     * 修改短信记录
     */
    @PreAuthorize("@ss.hasPermi('letterManage:letter:edit')")
    @Log(title = "短信记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyLetter wyLetter)
    {
        return toAjax(wyLetterService.updateWyLetter(wyLetter));
    }

    /**
     * 删除短信记录
     */
    @PreAuthorize("@ss.hasPermi('letterManage:letter:remove')")
    @Log(title = "短信记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{letterIds}")
    public AjaxResult remove(@PathVariable Long[] letterIds)
    {
        return toAjax(wyLetterService.deleteWyLetterByLetterIds(letterIds));
    }
    @PreAuthorize("@ss.hasPermi('letterManage:letter:list')")
    @PostMapping("/postLetter")
    public AjaxResult postLetter(@RequestBody WyLetter wyLetter){
        Long[] ownerList = wyLetter.getOwnerList();
        wyLetter.setUserName("1915854846@qq.com");
        for (Long ownerId : ownerList){
            WyOwner owner = wyOwnerService.selectWyOwnerByOwnerId(ownerId);
            wyLetter.setOwnerId(ownerId);
            MailUtil.sendMail(owner.getPhone(), wyLetter.getLetterInfo(), "通知");
        }
        AjaxResult add = add(wyLetter);
        return add;
    }


}
